<?php
   include_once dirname(__FILE__)."/mysql_connect.php";
	
   class GenerateSql
   {
      /*atributes*/
		private $sTable;
		private $sCommandType;
		private $aFieldName;
		private $aFieldValue;
		private $aWhere;
		private $sMensagemErro;
		private $sSql;

		/*constructor*/
		public function __contruct()
		{
			$this->aFieldName = array();
			$this->aFieldValue = array();
			$this->aWhere = array();
		}

		/*properties*/
		public function setTable($value)
		{
			$this->sTable = $value;
		}

		public function getTable()
		{
			return $this->sTable;
		}
        
		public function getMsgErro()
		{
			return $this->sMensagemErro;
		}

		public function setWhere($value)
		{
			$this->aWhere[] = $value;
		}

		public function setSql($value)
		{
			$this->sSql= $value;			
		}

		public function getSql()
		{
		   return $this->sSql;
		}
       
		public function setCommandType($value)
      {
         $this->sCommandType = $value; 
      }

		public function getCommandType()
		{
		   return $this->sCommandType;
		}
		
		/*methods*/
		public function execute()
		{
			switch ($this->sCommandType)
			{
				case Constante::CMD_INSERT:
					$this->sSql = $this->getSqlInsert();
					break;
				case Constante::CMD_DELETE:
					$this->sSql = $this->getSqlDelete();
					break;
				case Constante::CMD_UPDATE:
					$this->sSql = $this->getSqlUpdate();
					break;
			}			
			
			//echo $this->sSql;
			
			$result = mysql_query($this->sSql);

			if (! $result)
			{
				$this->sMensagemErro= mysql_error();
				return false;
			}
			
			return true;
		}

		public function getSqlDelete()
		{
			/* get where*/
			if (count($this->aWhere) != 0)
			{
				$sWhere = " WHERE ".$this->aWhere[0];
				for ($i = 1; $i < count($this->aWhere); $i++)
					$sWhere .= " AND " . $this->aWhere[i];
			}
			else $sWhere = " ";

			return "DELETE FROM ". $this->sTable ." ". $sWhere;
		}


		public function getSqlInsert()
		{
			$sSQLFields="";
			$sSQLValues="";

			if (count($this->aFieldValue) > 0)
			{
				$sSQLFields = $this->aFieldName[0];
				$sSQLValues = "'" . $this->aFieldValue[0] . "'";
				for ($i = 1; ($i < count($this->aFieldValue)) && ($i < count($this->aFieldName)); $i++)
				{
					if ( !$this->aFieldValue[$i] == "" )
					{
						$sSQLFields .= ", "  . $this->aFieldName[$i];
						$sSQLValues .= ", '" . $this->aFieldValue[$i] . "'";
					}
				}
			}
			return "INSERT INTO " . $this->sTable . " (" . $sSQLFields . ") VALUES (" . $sSQLValues . " )";
		}


		public function getSqlUpdate()
		{
			$sSql= "";
			$sWhere= "";

			/* get names and values*/
			if (count($this->aFieldValue) > 0)
			{
				$sSql = $this->aFieldName[0]."= '" . $this->aFieldValue[0] . "' ";
				for ($i = 1; ($i < count($this->aFieldValue)) && ($i < count($this->aFieldName)); $i++)
					//if ( !$this->aFieldValue[$i] == "" )
						$sSql .= ", "  . $this->aFieldName[$i]. "= '" . $this->aFieldValue[$i] . "'";
			}		
			
			/* get where*/
			if (count($this->aWhere) != 0)
			{
				$sWhere = " WHERE " . $this->aWhere[0];
				for ($i = 1; $i < count($this->aWhere); $i++)
					$sWhere .= " AND " . $this->aWhere[i];
			}
			else
				$sWhere = "";

			return "UPDATE " . $this->sTable . " SET " . $sSql . $sWhere;
		}


		public function prepare()
		{
			$this->sTable = "";
			$this->clearArrays();
			$this->sSql = "";
		}

		private function clearArrays()
		{
			for( $i=0; $i < count($this->aFieldName); $i++)
				unset($this->aFieldName[$i]);
			for( $i=0; $i < count($this->aFieldValue); $i++)
				unset($this->aFieldValue[$i]);				
			for( $i=0; $i < count($this->aWhere); $i++)				
				unset($this->aWhere[$i]);		
		}
		
		public function setValue($sValue, $sFieldName)
		{
			if (($sValue != 0) && ($sValue == ""))
				return;
			//echo $sValue." -> ".$sFieldName."<br>";
			$this->aFieldName [] = $sFieldName;
			$this->aFieldValue[] = $sValue;
		}
   }
?>
